package com.cxg.fairy.ec.main.index.ad;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * Created by CFairy
 */

public class RushBuyListDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {

        final ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        int size = 0;
        if (dataArray == null) {
            size = 0;
        } else {
            size = dataArray.size();
        }
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final String thumb = data.getString("thumbnail");
            final String itemType = data.getString("itemType");
            final String price = data.getString("price");
            final String defaultPrice = data.getString("linePrice");
            final int id = data.getInteger("skuId")==null?0:data.getInteger("skuId");
            final int spanSize = data.getInteger("spanSize");
            final String cartGoodsName = data.getString("name");
            final String unit = data.getString("units");

            int type = 0;
            int leftTime = 0;
            String rushStatus = "";
            if ("RUSHGOODS".equalsIgnoreCase(itemType)) {
                type = RushBuyListItemType.RUSH_LIST_VERTICAL;
            } else if ("RUSHGOODSTAG".equalsIgnoreCase(itemType)) {
                type = RushBuyListItemType.RUSH_BUY_LIST_HEADER;
                leftTime = data.getInteger("rushLeftTime");
                rushStatus = data.getString("rushStatus");
            }


            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ID, id)
                    .setField(ADGoodsListItemFields.PRICE, price)
                    .setField(ADGoodsListItemFields.DEFAULT_PRICE, defaultPrice)
                    .setField(ADGoodsListItemFields.AD_GOODS_NAME, cartGoodsName)
                    .setField(ADGoodsListItemFields.UNIT, unit)
                    .setField(ADGoodsListItemFields.POSITION, i)
                    .setField(ADGoodsListItemFields.SPAN_SIZE,spanSize)
                    .setField(ADGoodsListItemFields.THUMB, thumb)
                    .setField(ADGoodsListItemFields.RUSH_BUY_STATUS, rushStatus)
                    .setField(ADGoodsListItemFields.RUSH_BUY_TIME_LEFT, leftTime)
                    .build();

            dataList.add(entity);
        }

        return dataList;
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }


}
